package cn.javaxc.mvcwork.util.helper;
import java.util.*;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.mvcwork.util.common.*;
public class TreeHelper 
{ 
	public SQLList buildTree(SQLList node,TreeBean tb,Object pid,int level){

		SQLList arr=new SQLList();
		arr.setTreeBean(tb);
		if (level==0){
			List<SQLMap> children=MapListUtil.getDataList(node,pid,tb.getTreeNodeId());
			for (int i=0;i<children.size();i++ ){
				arr.add(buildTreeElement(node,tb,children,i,level));
			}	
		}
		if (level>100){
			return arr;
		}
		List<SQLMap> children=MapListUtil.getDataList(node,pid,tb.getTreeNodePid());
		for (int i=0;i<children.size();i++ ){
			if (tb.getPid()==null){
				arr.add(buildTreeElement(node,tb,children,i,level));
			}else{
				arr.add(buildTreeElement(node,tb,children,i,level+1));
			}	
			if (children.get(i).containsKey(tb.getTreeNodeId())){
				arr.addAll(buildTree(node,tb,children.get(i).getMap(tb.getTreeNodeId()),level+1));				
			}
		}
		return arr;
	}	

	private SQLMap buildTreeElement(SQLList node,TreeBean tb,List<SQLMap> children,int irow,int level){
		SQLMap v=children.get(irow);
		String p_html="";
		String html="";
		if (tb.getPrefix().equals("img")){
			html="|--——";
		}else{
			if (level>0){
				p_html="　";
			}			
			html="　";
		}
		for (int i=0;i<level-1;i++ ){	
			if (i==0){
				p_html+="│";
			}else{
				p_html+=html;
			}
			
		}

		if (irow == children.size() - 1) {
			p_html+= "└";
		}else{
			p_html+= "├";
		}
		if (MapListUtil.getDataList(node,v.getMap(tb.getTreeNodeId()),tb.getTreeNodePid()).size()>0){
			v.put("SON","a");
			v.put("tree_leaf",false);
		}else{
			v.put("tree_leaf",true);
		}
		if (tb.getPrefix().equals("img")){
			if ("a".equals(v.getMap("SON"))){
				p_html="<i class='ace-icon fa "+StringUtils.isEmpty(v.getMap(tb.getIcon()),"fa-folder")+"'></i>&nbsp;";
//				p_html="<IMG height=17 width=19 border=0 src='"+URLHelper.getIcon(v.getMap(tb.getIcon()),"foldersmall.gif")+"'>";
			}else{
				p_html="<i class='ace-icon fa "+StringUtils.isEmpty(v.getMap(tb.getIcon()),"fa-file-text")+"'></i>&nbsp;";
//				p_html="<IMG height=17 width=19 border=0 src='"+URLHelper.getIcon(v.getMap(tb.getIcon()),"page.gif")+"'>";
			}		
		}
		SQLMap emt = new SQLMap();
		emt.setCode(v.getMap(tb.getTreeNodeId()));
		emt.setDescription(p_html+v.getMap(tb.getTreeNodeName()));
		emt.putAll(v);
		emt.setTreeOutline(new TreeOutline(p_html,null,null,level+1));
		emt.put("tree_level",level);
		return emt;
	}

	public String getGantt(SQLList ganttlist,SQLList skips) {
		return "";
	}
};
